package com.nineclock.system.service.impl;

import com.nineclock.common.constant.NcConstant;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.entity.NcAuthority;
import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.SmsUtils;
import com.nineclock.common.utils.UUIDUtils;
import com.nineclock.system.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SmsUtils smsUtils;
    /**
     * 生成验证码存入redis中
     *
     * @param mobile
     * @param type
     */
    @Override
    public void getCode(String mobile, String type) {
        String code = UUIDUtils.getUUID().substring(0, 6);
        if (StringUtils.isBlank(mobile) || StringUtils.isBlank(type)) {
            throw new NcException(ResponseEnum.SEND_MESSAGE_ERROR);
        }

        //TODO 正则表达式判断手机号是否合法

        ValueOperations operations = redisTemplate.opsForValue();
        //注册
        if (SMSConstant.SMS_TYPE_REGISTER.equals(type)) {
            operations.set(SMSConstant.SMS_REGISTER_KEY_PREFIX + mobile, code, 5, TimeUnit.MINUTES);
        }
        //登录
        if (SMSConstant.SMS_TYPE_LOGIN.equals(type)) {
            operations.set(SMSConstant.SMS_LOGIN_KEY_PREFIX + mobile, code, 5, TimeUnit.MINUTES);
        }
        //修改主管理员
        if (SMSConstant.SMS_TYPE_CHANGE_MANAGER.equals(type)) {
            operations.set(SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX + mobile, code, 5, TimeUnit.MINUTES);
        }
        //修改密码
        if (SMSConstant.SMS_TYPE_RESET_PASSWORD.equals(type)) {
            operations.set(SMSConstant.SMS_RESET_KEY_PREFIX + mobile, code, 5, TimeUnit.MINUTES);
        }
        log.info("[手机号:" + mobile + "]");
        log.info("[验证码:" + code + "]");
//        smsUtils.sendSms(mobile,code);
    }

    /**
     * 验证码的效验
     * @param code
     * @param mobile
     * @param type
     * @return
     */
    @Override
    public boolean checkCode(String code, String mobile, String type) {
        if (StringUtils.isBlank(code)||StringUtils.isBlank(mobile)||StringUtils.isBlank(type)){
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        ValueOperations opsForValue = redisTemplate.opsForValue();
        String redisCode = null;
        //注册
        if (SMSConstant.SMS_TYPE_REGISTER.equals(type)) {
            redisCode = opsForValue.get(SMSConstant.SMS_REGISTER_KEY_PREFIX + mobile).toString();
            redisTemplate.delete(SMSConstant.SMS_REGISTER_KEY_PREFIX + mobile);
        }
        //登录
        if (SMSConstant.SMS_TYPE_LOGIN.equals(type)) {
            redisCode = opsForValue.get(SMSConstant.SMS_LOGIN_KEY_PREFIX + mobile).toString();
            redisTemplate.delete(SMSConstant.SMS_LOGIN_KEY_PREFIX + mobile);
        }
        //修改主管理员
        if (SMSConstant.SMS_TYPE_CHANGE_MANAGER.equals(type)) {
            redisCode = opsForValue.get(SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX + mobile).toString();
            redisTemplate.delete(SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX + mobile);
        }
        //修改密码
        if (SMSConstant.SMS_TYPE_RESET_PASSWORD.equals(type)) {
            redisCode = opsForValue.get(SMSConstant.SMS_RESET_KEY_PREFIX + mobile).toString();
            redisTemplate.delete(SMSConstant.SMS_RESET_KEY_PREFIX + mobile);
        }
        if (StringUtils.isBlank(redisCode)){
            throw new NcException(ResponseEnum.DATA_NOT_EXISTS);
        }
        if (!redisCode.equals(code)) {
            return false;
        }
        return true;
    }
}
